// 约定一个通用的键名
const TOKEN_KEY = 'token'

const USER_NAME = 'username'
const CART_DATA = 'cartdata'

// 获取个人信息
export const getToken = () => {
  const result = window.sessionStorage.getItem(TOKEN_KEY)

  return JSON.parse(result) // 如果已经获取到了数据，就转换json,否则token和userId就是空值
}

export const setToken = (info) => {
  window.sessionStorage.setItem(TOKEN_KEY, JSON.stringify(info))
}

export const removeToken = () => {
  window.sessionStorage.removeItem(TOKEN_KEY)
}

/* 用户名 */

// 获取用户名
export const getuserName = () => {
  const result = window.sessionStorage.getItem(USER_NAME)

  return JSON.parse(result)
}

// 设置用户名
export const setuserName = (info) => {
  window.sessionStorage.setItem(USER_NAME, JSON.stringify(info))
}
// 删除用户名
export const removeuserName = () => {
  window.sessionStorage.removeItem(USER_NAME)
}

/**
 * 购物车数据
 */

// 添加购物车数据
export const setCartData = (info) => {
  window.sessionStorage.setItem(CART_DATA, JSON.stringify(info))
}

// 获取购物车数据
export const getCartData = () => {
  window.sessionStorage.getItem(CART_DATA)
}
export const removeCartData = () => {
  window.sessionStorage.removeItem(CART_DATA)
}
